//
//  MyFavListVC.m
//  MeiXiu
//
//  Created by Yuan on 12-11-12.
//
//

#import "MyFavListVC.h"
#import "UIImageView+WebCache.h"
#import "HYProgressHUD.h"
#import "ImageViewCell.h"
#import "WaterFlowView.h"

@interface MyFavListVC ()<UIScrollViewDelegate,WaterFlowViewDelegate,WaterFlowViewDataSource>
{
    BOOL isRequesting;

    WaterFlowView *waterFlow;
    
    BOOL isEditMode;
}
@property(nonatomic,retain)WaterFlowView *waterFlow;

@property(nonatomic,retain)NSMutableArray *arrayData;


@end

@implementation MyFavListVC
@synthesize waterFlow = _waterFlow;
@synthesize arrayData = _arrayData;

- (id)init
{
    self = [super init];
    if (self)
    {
        self.arrayData = [[NSMutableArray alloc]initWithObjects:nil,nil];

        [self refresh];

    }
    return self;
}

-(void)dealloc
{
    [_arrayData release];_arrayData = nil;
    [_waterFlow release];_waterFlow = nil;
    [super dealloc];
}


-(void)loadView
{
    [super loadView];
    
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setTitle:HYLocalizedString(@"我的收藏") showBackButton:YES];
    
    self.view.backgroundColor = [UIColor colorWithRed:1.000 green:0.884 blue:0.950 alpha:1.000];
    
    self.waterFlow = [[WaterFlowView alloc] initWithFrame:CGRectMake(0,0,320,416)];
    self.waterFlow.waterFlowViewDelegate = self;
    self.waterFlow.waterFlowViewDatasource = self;
    self.waterFlow.delegate = self;
    self.waterFlow.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_waterFlow];

    [self setRightItem:HYLocalizedString(@"编辑") tag:0];

}

-(void)setRightItem:(NSString *)title tag:(NSInteger)tag
{
    UIImage *addImg =  ThemeImage(@"nav_btn_rightitem");
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.userInteractionEnabled = YES;
    btn.showsTouchWhenHighlighted = YES;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(editAction:) forControlEvents:UIControlEventTouchDown];
    btn.titleLabel.font=[UIFont fontWithName:@"msyh" size:14.0f];
    btn.frame = CGRectMake(0.0f, 0.0f,addImg.size.width,addImg.size.height);
    btn.tag = tag;
    [btn setBackgroundImage:addImg forState:UIControlStateNormal];
    [btn setBackgroundImage:addImg forState:UIControlStateHighlighted];
    UIBarButtonItem *item=[[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem =item;
    [item release];
}

-(void)editAction:(UIButton *)btn;
{
    if(btn.tag == 0)
    {
        isEditMode = YES;
        [self setRightItem:HYLocalizedString(@"完 成") tag:1];
        [_waterFlow reloadData];

    }
    else
    {
        isEditMode = NO;
        [self setRightItem:HYLocalizedString(@"编 辑") tag:0];
        [_waterFlow reloadData];

    }
}

-(void)refresh
{
    NSDictionary *p = [NSDictionary dictionaryWithObjectsAndKeys:
                                [[MSSetting sharedSetting]getAccountID],@"uid",
                                @"1",@"collect",nil];

    [WebService  getRequestAction:APIGetFavoritesList
                            param:p
                          success:^(NSDictionary *retid,NSError *error)
     {
         if (error == nil)
         {
             HYLog(@"%@",retid);
             NSArray *dataArray = [retid objectForKey:@"result"];
             if (dataArray!=nil && [dataArray isKindOfClass:[NSArray class]]&& [dataArray count]>0)
             {
                 [_arrayData addObjectsFromArray:dataArray];
                 [_waterFlow reloadData];
             }
         }
         [MSTool showError:error];
     } failure:^(id info ,NSError *error){
         [MSTool showError:error];
     } progress:self.navigationController.view];
}

-(void)removeFav:(UIButton *)btn
{
    ImageViewCell *view = (ImageViewCell*)[btn superview];
    int arrIndex = view.indexPath.row * _waterFlow.columnCount + view.indexPath.column;
    NSDictionary *dict = [_arrayData objectAtIndex:arrIndex];

    
}


#pragma mark WaterFlowViewDataSource
- (NSInteger)numberOfColumsInWaterFlowView:(WaterFlowView *)waterFlowView
{
    return 3;
}

- (NSInteger)numberOfAllWaterFlowView:(WaterFlowView *)waterFlowView{
    
    return [_arrayData count];
}

- (UIView *)waterFlowView:(WaterFlowView *)waterFlowView cellForRowAtIndexPath:(IndexPath *)indexPath
{
    ImageViewCell *view = [[ImageViewCell alloc] initWithIdentifier:nil];
    UIImage *img = [UIImage imageNamed:@"DeleteTheme_Button.png"];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:img forState:UIControlStateNormal];
    [btn setFrame:CGRectMake(78,5,img.size.width/2, img.size.height/2)];
    [btn addTarget:self action:@selector(removeFav:) forControlEvents:UIControlEventTouchUpInside];
    btn.tag = 100;
    [view addSubview:btn];
    return view;
}

-(void)waterFlowView:(WaterFlowView *)waterFlowView  relayoutCellSubview:(UIView *)view withIndexPath:(IndexPath *)indexPath
{
    int arrIndex = indexPath.row * waterFlowView.columnCount + indexPath.column;
    NSDictionary *object = [_arrayData objectAtIndex:arrIndex];
    
    ImageViewCell *imageViewCell = (ImageViewCell *)view;
    UIButton *btn = (UIButton *)[imageViewCell viewWithTag:100];
    btn.hidden = !isEditMode;
    
    imageViewCell.indexPath = indexPath;
    imageViewCell.columnCount = waterFlowView.columnCount;
    [imageViewCell relayoutViews];
    NSString *imageUrl = [object objectForKey:@"thumb"];
    if (imageUrl&&[imageUrl length]>0)
    {
        NSURL *URL = [NSURL URLWithString:imageUrl];
        [(ImageViewCell *)view setImageWithURL:URL placeholderImage:[UIImage imageNamed:@"lianxingdapeidefalutimage"]];
    }
    else
    {
        [(ImageViewCell *)view setImageWithURL:nil placeholderImage:[UIImage imageNamed:@"lianxingdapeidefalutimage"]];
    }
}

#pragma mark WaterFlowViewDelegate
- (CGFloat)waterFlowView:(WaterFlowView *)waterFlowView heightForRowAtIndexPath:(IndexPath *)indexPath
{
    int arrIndex = indexPath.row * waterFlowView.columnCount + indexPath.column;
    NSDictionary *dict = [_arrayData objectAtIndex:arrIndex];
    float width = 0.0f;
    float height = 0.0f;
    if (dict)
    {
        NSObject *width_ = [dict objectForKey:@"width"];
        if (width_ && ![width_ isKindOfClass:[NSNull class]] && [width_ isKindOfClass:[NSString class]])
        {
            width = [(NSString*)width_ floatValue];
        }
        else
        {
            width = 100;
        }
        
        NSObject *height_ = [dict objectForKey:@"height"];
        if (height_ && ![height_ isKindOfClass:[NSNull class]] && [height_ isKindOfClass:[NSString class]])
        {
            height = [(NSString*)height_ floatValue];
        }
        else
        {
            height = 100;
        }
    }
    return waterFlowView.cellWidth * (height/width);
}

- (void)waterFlowView:(WaterFlowView *)waterFlowView didSelectRowAtIndexPath:(IndexPath *)indexPath
{
    NSLog(@"indexpath row == %d,column == %d",indexPath.row,indexPath.column);
    int arrIndex = indexPath.row * waterFlowView.columnCount + indexPath.column;
    NSLog(@"%d",arrIndex);
    NSDictionary *item = [_arrayData objectAtIndex:arrIndex];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
